The Continue Statement
Normally, if a child script object and its parent both have handlers for the same command, the child uses its own handler. However, the handler in a child script object can handle a command first, and then use a Continue statement to call the handler for the same command in the parent.The use of a Continue statement to call a handler in a parent script object is called delegation. By delegating commands to a parent script object, a child can extend the behavior of a handler contained in the parent without having to repeat the entire handler definition. After the parent handles the command, AppleScript continues at the place in the child where the Continue statement was called. Handlers in child script objects that contain Continue statements are similar to wrapper methods in object-oriented programming.
The syntax of a Continue statement is
continue commandName parameterListwherecommandName is the name of the current command.
parameterList is the list of parameters to be passed with the command. The list must follow the same format as the parameter definitions in the handler definition for the command. For handlers with labeled parameters, this means that the parameter labels must match those in the handler definition. For handlers with positional parameters, the parameters must appear in the correct order. You can list actual values or parameter variables. If you list actual values, those values replace the parameter values that were specified in the original command. If you list parameter variables, the Continue statement passes the parameter values that were specified in the original command.
The following script includes two script object definitions similar to those shown in Figure 9-1 on page 273. The first,
Elizabeth
, works just like the scriptJohn
in the figure. The second,ChildOfElizabeth
, includes a handler with a Continue statement that is not included in the child script object (Simple
) shown in the figure.
script Elizabeth property HowManyTimes : 0 to sayHello to someone set HowManyTimes to HowManyTimes + 1 return "Hello " & someone end sayHello end script script ChildOfElizabeth property parent : Elizabeth on sayHello to someone if my HowManyTimes > 3 then return "No, I'm tired of saying hello." else continue sayHello to someone end if end sayHello end scriptIn the preceding example, the handler defined byChildOfElizabeth
for thesayHello
command checks the value of the HowManyTimes property each time the handler is run. If the value is greater than 3,ChildOfElizabeth
returns a message refusing to say hello. Otherwise,ChildOfElizabeth
calls thesayHello
handler in the parent script object (Elizabeth
), which returns the standard hello message. The wordsomeone
in the Continue statement is a parameter variable. It indicates that the parameter received with the originalsayHello
command will be passed to the handler in the parent script.
A Continue statement can change the parameters of a command before delegating it. For example, suppose the following script object is defined in
- Note
- The reserved word
my
in the statementif
my HowManyTimes > 10
in the previous example is required to indicate that HowManyTimes is a property of the script object. Without the wordmy
, AppleScript assumes that HowManyTimes is an undefined local variable.![]()
the same script as the preceding example. The first Continue statement changes the direct parameter of thesayHello
command from"Bill"
to"William"
. It does this by specifying the value"William"
instead of the parameter variablesomeone
.
script AnotherChildOfElizabeth property parent : Elizabeth on sayHello to someone if someone = "Bill" then continue sayHello to "William" else continue sayHello to someone end if end sayHello end scriptIf you override a parent's handler in this manner, the reserved wordsme
andmy
in the parent's handler no longer refer to the parent, as demonstrated in the example that follows.
script Hugh on identify() me end identify end script script Andrea property parent : Hugh on identify() continue identify() end identify end script tell Hugh to identify() --result: <<script Hugh>> tell Andrea to identify() --result: <<script Andrea>>